package dbTest;
import java.sql.*;

public class jdbc1 {
    // JDBC driver name and database URL
    static final String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver";
    static final String DB_URL = "jdbc:mysql://47.109.25.19:3306/online_store";

    //  Database credentials
    static final String USER = "root";
    static final String PASS = "racksys123";

    public static void main(String[] args) {
        Connection connection = null;
        Statement statement = null;
        try{
            //STEP 2: Register JDBC driver
            Class.forName(JDBC_DRIVER);

            //STEP 3: Open a connectionection
            System.out.println("connectionecting to a selected database...");
            connection = DriverManager.getConnection(DB_URL, USER, PASS);

            System.out.println("connectionected database successfully...");

            //STEP 4: Execute a query
            System.out.println("Inserting records into the table...");
            statement = connection.createStatement();

            for(int i =0; i<100_0000;i++){
                String sql = "INSERT INTO product (pname,price) VALUES (\"pname1\", 33333.5)";
                statement.executeUpdate(sql);
                System.out.println("Inserted records into the table, count:" + i);
            }
        }catch(SQLException e){
            //Handle errors for JDBC
            e.printStackTrace();
        }catch(Exception e){
            //Handle errors for Class.forName
            e.printStackTrace();
        }finally{
            //finally block used to close resources
            try{
                if(statement != null){
                    connection.close();
                }
            }catch(SQLException e){
            }
            try{
                if(connection != null){
                    connection.close();
                }
            }catch(SQLException e){
                e.printStackTrace();
            }
        }

        System.out.println("end");
    }
}
